#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    int ret;
    char rbuf[32] = {0};
    char wbuf[32] = {"0123456789 0123456789 012345678"};

    fd = open("/dev/iolearn0", O_RDWR | O_NONBLOCK);
    if (-1 == fd)
        goto fail;

    ret = read(fd, rbuf, sizeof(rbuf));
    if (ret < 0)
        perror("first read");

    ret = write(fd, wbuf, sizeof(wbuf));
    if (ret < 0)
        perror("first write");

    ret = write(fd, wbuf, sizeof(wbuf));
    if (ret < 0)
        perror("second write");

    ret = read(fd, rbuf, sizeof(rbuf));
    if (read < 0)
        perror("second write");

    printf("rbuf: %s\n", rbuf);
    close(fd);
    exit(EXIT_SUCCESS);

fail:
    perror("non-block");
    exit(EXIT_FAILURE);
}